/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.common;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Locale;
import org.apache.log4j.Logger;

/**
 *
 * @author sorasaks
 */
public class FileUtil {
    
    private static Logger LOG = Logger.getLogger(FileUtil.class);
    
    public static void createFile(String filename, StringBuilder text) {
        try {
            File file = new File(filename);
            
            LOG.info("::::::::::::::::::::: createFile = " + file.getAbsolutePath());

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }
            
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(text.toString());
            bw.close();
            
            System.out.println("Done");
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
            e.printStackTrace();
        }
    }
    
    public static void appandText(String filename, String text) {
        try {
            
            File file = new File(filename);

            //if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            //true = append file
            FileWriter fileWritter = new FileWriter(file.getAbsoluteFile(), true);
            BufferedWriter bufferWriter = new BufferedWriter(fileWritter);
            PrintWriter writer = new PrintWriter(bufferWriter);
            writer.println(text);
            writer.close();
            
            LOG.info("::::::::::::::: " + text + " :::::: DONE");
            
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
            e.printStackTrace();
        }
    }
    
    public static void uploadExcel(InputStream inputStream,
            String outputFile) throws Exception {
        
        String path = "excels";
        File pathFile = new File(path);
        
        if (!pathFile.exists()) {
            pathFile.mkdir();
        }
        
        path = path + "/" + ConvertFormatUtil.convertFormat(new Date(), ConvertFormatUtil.DEFAULT_DATE_FORMAT, Locale.US);
        
        pathFile = new File(path);
        if (!pathFile.exists()) {
            pathFile.mkdir();
        }
        
        OutputStream outputStream = new FileOutputStream(new File(path + "/" + outputFile));
        try {
            byte[] buffer = new byte[1024];
            for (int count; (count = inputStream.read(buffer)) != -1;) {
                outputStream.write(buffer, 0, count);
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        } finally {
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        }
    }
    
    public static void deleteFile(String filename) {
        File file = new File(filename);
        
        if (file.exists()) {
            file.delete();
        }
    }
}
